package com.tjmc.nsia.core.schedule;

import com.tjmc.nsia.core.utils.SpringUtils;
import com.tjmc.nsia.core.utils.Threads;

import javax.annotation.PreDestroy;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Luc
 * @version 1.0
 * @date 2024-06-06 17:18
 */
public class AsyncManager {

    private final int OPERATE_DELAY_TIME = 10;

    private ScheduledExecutorService executor = (ScheduledExecutorService) SpringUtils.getBean("scheduledExecutorService");

    private AsyncManager() {
    }

    private static AsyncManager me = new AsyncManager();

    public static AsyncManager me() {
        return me;
    }

    public void execute(TimerTask task) {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }

    @PreDestroy
    public void shutdown() {
        Threads.shutdownAndAwaitTermination(executor);
    }
}
